package com.ruoyi.web.controller.company.enterprise;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.company.study.domain.ComStudy;
import com.ruoyi.company.study.service.IComStudyService;
import com.ruoyi.company.uploadfile.domain.SysFileInfo;
import com.ruoyi.company.uploadfile.service.ISysFileInfoService;
import com.ruoyi.web.controller.common.CommonController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.company.datainfo.domain.ComDataInfo;
import com.ruoyi.company.datainfo.service.IComDataInfoService;
import com.ruoyi.company.dynamic.domain.ComDynamic;
import com.ruoyi.company.dynamic.service.IComDynamicService;
import com.ruoyi.company.notice.domain.ComNotice;
import com.ruoyi.company.notice.service.IComNoticeService;

import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 进入网站前端首页
 * @author baike
 *
 */

@Controller
@RequestMapping("/enterprise")
public class EntIndexController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(CommonController.class);
	
	private String prefix = "company/website";
	
	@Autowired
    private IComDynamicService comDynamicService;

	@Autowired
	private IComStudyService comStudyService;
	
	@Autowired
    private IComNoticeService comNoticeService;
	
	@Autowired
    private IComDataInfoService comDataInfoService;
	
	@Autowired
    private ISysFileInfoService sysFileInfoService;
	
	/**
	 * 公司首页
	 * @return
	 */
	@RequestMapping("/index")
	public String index(ModelMap mmap) {
		List<ComDynamic> dynamicDouble = comDynamicService.getDynamicDouble();
		mmap.put("dynamicDouble", dynamicDouble);
		return prefix + "/index";
	}
	
	/**
	 * 公司简介
	 * @return
	 */
	@RequestMapping("/brief")
	public String enterpriseBrief() {
		
		return prefix + "/brief";
	}
	
	/**
	 * 公司动态
	 * @return
	 */
	@RequestMapping("/dynamic")
	public String trends(ModelMap mmap) {
		return prefix + "/trends";
	}
	
	/**
	 * 公司动态列表
	 * @return
	 */
	@RequestMapping("/dynamicList")
	@ResponseBody
	public TableDataInfo dynamicList(){
		startPage();
		List<ComDynamic> dynamicList = comDynamicService.getDynamicList();
		return getDataTable(dynamicList);
		
	}

	/**
	 * 学习培训跳转页
	 * @return
	 */
	@RequestMapping("/studyYard")
	public String studyYard() {

		return prefix + "/studyYard";
	}
	/**
	 * 学习培训列表
	 * @return
	 */
	@PostMapping("/studyYardlist")
	@ResponseBody
	public TableDataInfo studyYardlist(ComStudy comStudy)
	{
		startPage();
		List<ComStudy> list = comStudyService.selectComStudyList(comStudy);
		return getDataTable(list);
	}
	/**
	 * 公告管理
	 * @param mmap
	 * @return
	 */
	@RequestMapping("/notice")
	public String notice(ModelMap mmap) {
		return prefix + "/notice";
	}
	
	/**
	 * 公告管理列表
	 * @return
	 */
	@RequestMapping("/noticeList")
	@ResponseBody
	public TableDataInfo noticeList(){
		startPage();
		List<ComNotice> noticeList = comNoticeService.getNoticeList();
		return getDataTable(noticeList);
		
	}
	
	/**
	 * 数据信息
	 * @param mmap
	 * @return
	 */
	@RequestMapping("/datainfo")
	public String dataInfo(ModelMap mmap) {
		return prefix + "/datainfo";
	}
	
	/**
	 * 数据信息列表
	 * @return
	 */
	@RequestMapping("/dataInfoList")
	@ResponseBody
	public TableDataInfo dataInfoList(){
		startPage();
		List<ComDataInfo> dataInfoList = comDataInfoService.getDataInfoList();
		return getDataTable(dataInfoList);
		
	}
	/**
	 * 数据信息详情、数据信息附件
	 * @param noticeId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/dataInfoDetail/{dataId}")
	//@ResponseBody
	public String dataInfoDetail(@PathVariable("dataId") String dataId,ModelMap mmap) {
		//数据信息详情
		ComDataInfo comDataInfo = comDataInfoService.selectComDataInfoById(dataId);
		//数据信息附件
		List<SysFileInfo> annexList=getAnnexList(dataId);
		mmap.put("comDataInfo", comDataInfo);
		mmap.put("annexList", annexList);
		if(annexList.size()!=0){
			mmap.put("flag", 1);
		}else{
			mmap.put("flag", 0);
		}
		return prefix + "/dataInfoDetail";
	}
	
	/**
	 * 公告管理详情、公告管理详情
	 * @param noticeId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/noticeDetail/{noticeId}")
	//@ResponseBody
	public String noticeDetail(@PathVariable("noticeId") String noticeId,ModelMap mmap) {
		//公告管理详情
		ComNotice comNotice = comNoticeService.selectComNoticeById(noticeId);
		//公告管理详情
		List<SysFileInfo> annexList=getAnnexList(noticeId);
		mmap.put("comNotice", comNotice);
		mmap.put("annexList", annexList);
		if(annexList.size()!=0){
			mmap.put("flag", 1);
		}else{
			mmap.put("flag", 0);
		}
		return prefix + "/noticeDetail";
	}

	/**
	 * 公司动态详情页、附件查询
	 * @param dynamicId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/dynamicDetail/{dynamicId}")
	//@ResponseBody
	public String trends(@PathVariable("dynamicId") String dynamicId,ModelMap mmap) {
		//公司动态详情
		ComDynamic dynamic = comDynamicService.selectComDynamicById(dynamicId);
		//公司动态附件
		List<SysFileInfo> annexList = getAnnexList(dynamicId);
		mmap.put("dynamic", dynamic);
		mmap.put("annexList", annexList);
		if(annexList.size()!=0){
			mmap.put("flag", 1);
		}else{
			mmap.put("flag", 0);
		}
		return prefix + "/dynamicDetail";
	}
	
	/***
	 * 查询附件的公用方法
	 * @param relationId 附件关联Id
	 * @return
	 */
	public List<SysFileInfo> getAnnexList(String relationId){
		List<SysFileInfo> annexList=sysFileInfoService.getDynamicAnnex(relationId);
		return annexList;
	}
	/**
	 * 学习培训详情页面
	 * @return
	 */
	@GetMapping("/studyDetail/{studyId}")
	public String studyDetail(@PathVariable("studyId") String studyId, ModelMap mmap)
	{
		ComStudy comStudy = comStudyService.selectComStudyById(studyId);
		mmap.put("comStudy",comStudy);
		//公告管理详情
		List<SysFileInfo> annexList=getAnnexList(studyId);
		mmap.put("annexList", annexList);
		if(annexList.size()!=0){
			mmap.put("flag", 1);
		}else{
			mmap.put("flag", 0);
		}
		return prefix + "/studyDetail";
	}
	
	/**
	 * 附件下载
	 * @param fileId
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@GetMapping("/download/{fileId}")
	public void resourceDownload(@PathVariable("fileId") String fileId, HttpServletRequest request, HttpServletResponse response)
			throws Exception
	{
		SysFileInfo fileInfo = sysFileInfoService.selectSysFileInfoById(fileId);
		String resource = fileInfo.getFilePath();
		// 本地资源路径
		String localPath = RuoYiConfig.getDownloadPathTmp();
		// 数据库资源地址
		String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX_TEMP);
		// 下载名称
		String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition",
				"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
		FileUtils.writeBytes(downloadPath, response.getOutputStream());
	}
	
	
}
